package com.leyou.auth.config;

import com.leyou.common.auth.utils.RsaUtils;
import lombok.Data;
import org.bouncycastle.jcajce.provider.asymmetric.rsa.RSAUtil;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;

import javax.annotation.PostConstruct;
import java.security.PrivateKey;
import java.security.PublicKey;

@Data
@ConfigurationProperties(prefix = "ly.jwt")
public class JwtProperties implements InitializingBean {
    private String pubKeyPath;//: F:\itcast-work\heima-tianjin-2\ssh\id_rsa.pub # 公钥地址
    private String priKeyPath;//: F:\itcast-work\heima-tianjin-2\ssh\id_rsa # 私钥地址

    private PublicKey publicKey;
    private PrivateKey privateKey;

    private UserToken userToken = new UserToken();

    @Data
    public class UserToken{
        private Integer expire;//: 30 # 过期时间,单位分钟
        private String cookieName;//:;// LY_TOKEN # cookie名称
        private String cookieDomain;//: leyou.com # cookie的域
    }
    /**
     * 当前类构造完成后，执行这个方法
     */
//    @PostConstruct
//    public void init() throws Exception {
//        publicKey  = RsaUtils.getPublicKey(pubKeyPath);
//        privateKey = RsaUtils.getPrivateKey(priKeyPath);
//    }

    /**
     * 使用springbean的生命周期
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        publicKey  = RsaUtils.getPublicKey(pubKeyPath);
        privateKey = RsaUtils.getPrivateKey(priKeyPath);
    }
}
